package com.ocl.www.controller;

import com.ocl.www.domain.LyUser;
import com.ocl.www.security.ShiroCasRealmImpl;
import com.ocl.www.service.LyUserService;
import com.ocl.www.utils.ResourceBundleUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/31.
 */


@Controller
public class MainPageController {

    private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ShiroCasRealmImpl.class);


    @Autowired
    private LyUserService lyUserService;

//    @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ModelAndView index(Map<String,Object> map){
//
//        return new ModelAndView("lanyou/index");
//
//    }
    @RequestMapping(value = "/apptoweb", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ModelAndView apptoweb(Map<String,Object> map){

        return new ModelAndView("index");

    }

    @RequestMapping(value = "/jumpage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String jumpage(Map<String,Object> map, HttpServletRequest request){

        //权限校验。判断是否包含权限。
        Subject subject = SecurityUtils.getSubject();

        try {
            String userId = SecurityUtils.getSubject().getPrincipals().asList().get(0).toString();
            LyUser lyUser =new LyUser();
            lyUser.setUserId(userId);

            lyUser = lyUserService.getUserInfo(lyUser);

            if (lyUser==null){
                lyUser =new LyUser();
                lyUser.setUserId(userId);
                lyUser.setUserName(userId);
                lyUser.setUserImg("\\upload\\nimingtouxiang.jpg");
                lyUserService.addUser(lyUser);
            }
        }catch(NullPointerException e){

        }catch (Exception e){

        }
        //具体响应ShiroDbRealm。doGetAuthorizationInfo，判断是否包含此url的响应权限
//        boolean isPermitted = subject.isPermitted("注册");
//        if(isPermitted == true) {
//            log.info("该用户拥有此权限：注册");
//            log.info("此处应该跳转到原来的页面！");
//
//        }else{
//            log.info("该用户没有此权限：注册");
//            log.info("返回登录页面！");
//
//        }
        String pageinfo = String.valueOf(request.getSession().getAttribute("page"));
        String pageId = String.valueOf(request.getSession().getAttribute("pageId"));
        if ("blog".equals(pageinfo)){
            pageinfo = ResourceBundleUtils.getProperty("project.server.host")+"/blog.html?id="+pageId;
        }
        if ("index".equals(pageinfo)){
            pageinfo = ResourceBundleUtils.getProperty("project.server.host");
        }
        return "redirect:" + pageinfo;

    }




}
